package com.hfh.main.report.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hfh.main.common.annotate.JwtToken;
import com.hfh.main.common.resp.Result;
import com.hfh.main.report.entity.WeekEntity;
import com.hfh.main.report.mapper.DailyReportMapper;
import com.hfh.main.report.service.WeekService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 日报 前端控制器
 * </p>
 */
@Tag(name="日报管理")
@RestController
@RequestMapping("/weekEntity")
public class WeekController {

    @Autowired
    private DailyReportMapper dailyReportMapper;
    @Autowired
    private WeekService weekService;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @JwtToken
    @Operation(summary = "周-查询-编号")
    @PostMapping("/selectDailyReportByID/{param}")
    public Result<?> selectDailyReportByID(@PathVariable @Validated String param) {
        QueryWrapper<WeekEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(("id"), param);
        if (weekService.count(wrapper) == 0) return Result.failure(500, "编号不存在", sdf.format(new Date()));
        return Result.success(weekService.getOne(wrapper));
    }

    @JwtToken
    @Operation(summary = "周-保存")
    @PostMapping("/insert")
    public Result<?> insert(@RequestBody @Validated WeekEntity entity) {
        QueryWrapper<WeekEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(("id"), entity.getId());
        if (weekService.count(wrapper) != 0) return Result.failure(500, "编号已存在", sdf.format(new Date()));
        weekService.save(entity);
        return Result.success("保存成功", sdf.format(new Date()));
    }

    @JwtToken
    @Operation(summary = "周-删除")
    @PostMapping("/delete/{param}")
    public Result<?> delete(@PathVariable @Validated String param) {
        QueryWrapper<WeekEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(("id"), param);
        if (weekService.count(wrapper) == 0) return Result.failure(500, "编号不存在", sdf.format(new Date()));
        weekService.remove(wrapper);
        return Result.success("保存成功", sdf.format(new Date()));
    }

    @JwtToken
    @Operation(summary = "周-修改")
    @PostMapping("/update")
    public Result<?> update(@RequestBody @Validated WeekEntity entity) {
        QueryWrapper<WeekEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(("id"), entity.getId());
        if (weekService.count(wrapper) == 0) return Result.failure(500, "编号不存在", sdf.format(new Date()));
        weekService.updateById(entity);
        return Result.success("修改成功", sdf.format(new Date()));
    }

}
